package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.*;
import com.yl.auth.model.entity.Chuangwei;
import com.yl.auth.model.entity.Laoren;
import com.yl.auth.model.vo.TiwenVo;
import com.yl.auth.model.vo.XuetangVo;
import com.yl.auth.service.*;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ln
 * @since 2023-06-19
 */
@RestController
@RequestMapping("/auth/tiwen")
@Api(tags = {"体温"})
public class TiwenController {

    @Resource
    private TiwenService tiwenService;
    @Resource//护工
    private HugongService ylHugongService;
    @Resource//楼层
    private LoucengService ylFloorService;
    @Resource//房间
    private FangjianService ylRoomService;
    @Resource//床位
    private ChuangweiService ylBedService;
    @Resource//老人
    private LaorenService laorenService;
    @ApiOperation(value = "查询体温")
    @GetMapping("/findAllPage/{page}/{limit}")
    public Result findAllPage(@ApiParam(name = "page",value = "当前页码",required = true)
                              @PathVariable Long page,
                              @ApiParam(name = "limit",value = "每页记录数",required = true)
                              @PathVariable Long limit,
                              @RequestBody(required = false) TiwenVo tiwenVo){
        Page<TiwenVo> pageParam = new Page<>(page,limit);
        IPage<TiwenVo> pageModel = tiwenService.findAllPage(pageParam,tiwenVo);
        List<TiwenVo> list = pageModel.getRecords();
        for(TiwenVo tiwen: list){
            tiwen.setWeizhi(String.join("/",tiwen.getLouceng(),tiwen.getFangjian(),tiwen.getChuangwei()));
        }
        return Result.ok(pageModel);
    }

    @GetMapping("/findPage/{page}/{limit}")
    public Result findPage(@PathVariable Long page,
                           @PathVariable Long limit,
                           Xueya xueya){
        List<Louceng> flist = ylFloorService.list();
        List<Fangjian> rlist = ylRoomService.list();
        List<HugongBean> hlist = ylHugongService.list();
        List<Chuangwei> blist = ylBedService.list();
        List<Tiwen> tlist = tiwenService.list();
        List<Laoren> elist = laorenService.list();
        Page<Xueya> pageParam = new Page<>(page,limit);
        QueryWrapper<Xueya> wrapper = new QueryWrapper<>();
        for(int i=0;i<tlist.size();i++){
            for (int k=0;k<hlist.size();k++){
                if(hlist.get(k).getId()==tlist.get(i).getHugongid()){
                    tlist.get(i).setHname(hlist.get(k).getHname());//查询出护工名并赋值
                }
            }
            for (int l=0;l<elist.size();l++){
                if(tlist.get(i).getLaorenid()==elist.get(l).getId()){
                    tlist.get(i).setLname(elist.get(l).getName());//查询出老人名
                }
            }
        }
        for(int k=0;k<elist.size();k++){//老人集合循环
            for (int l=0;l<blist.size();l++){//床位集合循环
                if (elist.get(k).getBedId()==blist.get(l).getId()){
                    tlist.get(k).setChuang(blist.get(l).getBedNo());//根据老人的床位id查询老人居住床位
                }
            }
        }
        for(int m=0;m<blist.size();m++){
            for (int z=0;z<rlist.size();z++){
                if (blist.get(m).getRoomId()==rlist.get(z).getId()){
                    tlist.get(m).setFang(String.valueOf(rlist.get(z).getRoomNo()));//根据床位查询出房间
                }
            }
        }
        for (int a=0;a<rlist.size();a++){
            for(int b=0;b<flist.size();b++){
                if (rlist.get(a).getFloorId()==flist.get(b).getId()){
                    tlist.get(a).setWeizhi(String.join("/",flist.get(b).getName(),tlist.get(a).getFang(),tlist.get(a).getChuang()));//根据房间查询出楼层
                }
            }
        }
        return Result.ok(tlist);
    }

    @GetMapping("/getBed")
    public Result getBed(){
        List<Louceng> flist = ylFloorService.list();
        List<Fangjian> rlist = ylRoomService.list();
        List<Chuangwei> blist = ylBedService.list();
        List<String> list = new ArrayList<>();
        String fang = "";
        String chuang ="";
        for(int m=0;m<blist.size();m++){
            for (int z=0;z<rlist.size();z++){
                if (blist.get(m).getRoomId()==rlist.get(z).getId()){
                    for(int i=0;i<flist.size();i++){
                        if(flist.get(i).getPid()==1){
                            if (rlist.get(z).getFloorId()==flist.get(i).getWei()){
                                list.add(String.join("/",flist.get(i).getName(),String.valueOf(rlist.get(z).getRoomNo()),blist.get(m).getBedNo()));
                            }
                        }
                    }
                }
            }
        }
        return Result.ok(list);
    }
}

